package com.zeta.eduservice.service.impl;


import com.zeta.eduservice.entity.question.EduPaper;
import com.zeta.eduservice.entity.question.EduPaperAnswer;
import com.zeta.eduservice.entity.question.EduPaperContentAnswer;
import com.zeta.eduservice.entity.vo.PaperAnswerVo;
import com.zeta.eduservice.entity.vo.PaperVo;
import com.zeta.eduservice.mapper.EduPaperAnswerMapper;
import com.zeta.eduservice.service.EduPaperAnswerService;
import com.zeta.eduservice.service.EduPaperContentAnswerService;
import com.zeta.eduservice.service.EduPaperService;
import com.zeta.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.awt.print.Paper;
import java.io.Serializable;

import org.apache.poi.wp.usermodel.Paragraph;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 试卷答案 服务实现类
 * </p>
 *
 * @author zeta
 * @since 2020-08-16
 */
@Service
public class EduPaperAnswerServiceImpl extends ServiceImpl<EduPaperAnswerMapper, EduPaperAnswer> implements EduPaperAnswerService {
@Autowired
EduPaperAnswerService paperAnswerService;
	@Autowired
	EduPaperService paperService;
	@Autowired
	private EduPaperContentAnswerService papercontentAnswerService;
	@Override
	public  String savePaperAnsewer(PaperAnswerVo vo) {
		
		 EduPaperAnswer eduPaperAnswer = new EduPaperAnswer();
		 BeanUtils.copyProperties(vo, eduPaperAnswer);
	
		 EduPaperAnswer selectById = baseMapper.selectById(eduPaperAnswer.getId());
	
			 boolean re = this.save(eduPaperAnswer);
				if(!re) {
					throw new GuliException(20001,"添加试卷答案失败");
				}
				EduPaperContentAnswer content=new EduPaperContentAnswer();
				content.setContent(vo.getContent());
				content.setId(vo.getId());
				boolean re2 = papercontentAnswerService.save(content);
				if(!re2) {
					throw new GuliException(20001,"试卷答案内容保存失败");
				}
				
				
				EduPaper byId = paperService.getById(vo.getId());
				byId.setIshaveanswer(1);
				paperService.updateById(byId);
				return vo.getId();
	
	
	}
	@Override
	public PaperAnswerVo getPaperAnswer(Serializable id) {
		EduPaperAnswer selectById = baseMapper.selectById(id);
		EduPaperContentAnswer courseDescription = papercontentAnswerService.getById(id);
		PaperAnswerVo courseInfoForm =new PaperAnswerVo();
		if(selectById!=null) {
			BeanUtils.copyProperties(selectById, courseInfoForm);
		}
		
		courseInfoForm.setContent(courseDescription.getContent());
		return courseInfoForm;
	}
	@Override
	public void updatePaperAnswerForm(PaperAnswerVo courseInfoForm) {
		EduPaperAnswer eduCourse = new EduPaperAnswer();
		BeanUtils.copyProperties(courseInfoForm, eduCourse);
		boolean i = paperAnswerService.updateById(eduCourse);
		if(i==false)
			throw new GuliException(20001,"修改失败");
		EduPaperContentAnswer eduCourseDescription = new EduPaperContentAnswer();
		eduCourseDescription.setId(courseInfoForm.getId());
		eduCourseDescription.setContent(courseInfoForm.getContent());
		papercontentAnswerService.updateById(eduCourseDescription);
		
	}

	@Override
	public boolean removeByIdpaper(Serializable id) {
		boolean removeById = papercontentAnswerService.removeById(id);

		  Integer result = baseMapper.deleteById(id);
		 EduPaper byId = paperService.getById(id);
		 byId.setIshaveanswer(0);
		 paperService.updateById(byId);
	      return null != result && result > 0;
	}



}
